<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'shop:sell:shopProductSell:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
        <a-popconfirm
          title="是否确认发布？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handlePublish(1)"
        >
          <a-button type="success" v-auth="'shop:sell:shopProductSell:fb'">
            <Icon icon="simple-line-icons:paper-plane" /> {{ t('发布') }}
          </a-button>
        </a-popconfirm>
        <a-popconfirm
          title="是否确认取消发布？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handlePublish(0)"
        >
          <a-button type="error" v-auth="'shop:sell:shopProductSell:fb'">
            <Icon icon="simple-line-icons:action-undo" /> {{ t('取消发布') }}
          </a-button>
        </a-popconfirm>
        <a-popconfirm
          title="是否确认关闭？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleClose(1)"
        >
          <a-button type="warning" v-auth="'shop:sell:shopProductSell:close'">
            <Icon icon="simple-line-icons:close" /> {{ t('关闭') }}
          </a-button>
        </a-popconfirm>
        <a-popconfirm
          title="是否确认关闭？"
          ok-text="是"
          cancel-text="否"
          placement="bottom"
          @confirm="handleClose(0)"
        >
          <a-button type="success" v-auth="'shop:sell:shopProductSell:close'">
            <Icon icon="simple-line-icons:check" /> {{ t('开启') }}
          </a-button>
        </a-popconfirm>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.ctitle }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <!-- 页签方式编辑 -->
    <InputFormTabs @register="registerDrawer2" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopSellShopProductSellList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    shopProductSellDelete,
    shopProductSellListData,
    updateFbStatus,
    openOrCloseSell,
  } from '/@/api/shop/sell/shopProductSell';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import InputFormTabs from './formTabs.vue';

  const { t } = useI18n('shop.sell.shopProductSell');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('活动管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('活动标题'),
        field: 'ctitle',
        component: 'Input',
      },
      {
        label: t('活动简介'),
        field: 'simpleContent',
        component: 'Input',
      },
      {
        label: t('活动开始'),
        field: 'beginDate',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
        },
      },
      {
        label: t('活动结束'),
        field: 'endDate',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          showTime: false,
        },
      },
      {
        label: t('促销包名称'),
        field: 'abbName',
        component: 'Input',
      },
      {
        label: t('主产品'),
        field: 'mainInv',
        component: 'Input',
      },
      {
        label: t('发布状态'),
        field: 'fbStatus',
        component: 'Select',
        componentProps: {
          dictType: 'shop_fb_status',
          allowClear: true,
        },
      },
      {
        label: t('是否关闭'),
        field: 'bclose',
        component: 'Select',
        componentProps: {
          dictType: 'sys_yes_no',
          allowClear: true,
        },
      },
    ],
    fieldMapToTime: [
      ['beginDate', ['beginDate_gte', 'beginDate_lte']],
      ['endDate', ['endDate_gte', 'endDate_lte']],
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('活动标题'),
      dataIndex: 'ctitle',
      key: 'a.ctitle',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('活动简介'),
      dataIndex: 'simpleContent',
      key: 'a.simple_content',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('活动详情'),
      dataIndex: 'content',
      key: 'a.content',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('缩略图地址'),
      dataIndex: 'picUrl',
      key: 'a.pic_url',
      sorter: true,
      width: 220,
      align: 'left',
    },
    {
      title: t('活动开始'),
      dataIndex: 'beginDate',
      key: 'a.begin_date',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('活动结束'),
      dataIndex: 'endDate',
      key: 'a.end_date',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('促销包名称'),
      dataIndex: 'abbName',
      key: 'a.abb_name',
      sorter: true,
      width: 200,
      align: 'left',
    },
    {
      title: t('主产品'),
      dataIndex: 'basInv.cinvname',
      key: 'a.main_inv',
      sorter: true,
      width: 280,
      align: 'left',
    },
    {
      title: t('起订量'),
      dataIndex: 'moq',
      key: 'a.moq',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('单价'),
      dataIndex: 'fprice',
      key: 'a.fprice',
      sorter: true,
      width: 100,
      align: 'right',
    },
    {
      title: t('单位'),
      dataIndex: 'unitName',
      key: 'a.unit_name',
      sorter: true,
      width: 100,
      align: 'center',
    },
    {
      title: t('换算率'),
      dataIndex: 'ichangrate',
      key: 'a.ichangrate',
      sorter: true,
      width: 100,
      align: 'right',
    },
    {
      title: t('发布状态'),
      dataIndex: 'fbStatus',
      key: 'a.fb_status',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'shop_fb_status',
    },
    {
      title: t('是否关闭'),
      dataIndex: 'bclose',
      key: 'a.bclose',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'sys_yes_no',
    },
    {
      title: t('关闭日期'),
      dataIndex: 'closeDate',
      key: 'a.close_date',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('创建日期'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 150,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑活动'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'shop:sell:shopProductSell:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除活动'),
        popConfirm: {
          title: t('是否确认删除活动'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'shop:sell:shopProductSell:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerDrawer2, { openDrawer: openDrawer2 }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: shopProductSellListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer2(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await shopProductSellDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  // 发布/取消发布
  async function handlePublish(ope) {
    if (getSelectRows().length) {
      var selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.id + ',';
      });
      const res = await updateFbStatus({ selIds: selIds.value, ope: ope });
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请先选择一行数据！');
    }
  }

  // 开启/关闭
  async function handleClose(ope) {
    if (getSelectRows().length) {
      var selIds = ref('');
      getSelectRows().forEach((item) => {
        selIds.value += item.id + ',';
      });
      const res = await openOrCloseSell({ selIds: selIds.value, ope: ope });
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请先选择一行数据！');
    }
  }

  function handleSuccess() {
    reload();
  }
</script>
